var commonFunc = require('../../lib/system/common')
var tools = require('../../lib/system/tools')
var auth = require('../../lib/auth/login')
var businessStorage = require('../../lib/common/storage')
var commonConstant = require('../../lib/common/constant')
var pages = require('../../lib/common/pageConstant')

Page({
    data: {
        goodsSum: 1,
        goodsMinusBtnFlag: '1',
        commonConstant: commonConstant,
        goodsDetail: {},
        expressTitle: '',
        couponBtnFlashStatus: false,
        couponBtnColorFlag: 0,
        couponBtnColor: 'normal',
        expressBoxShow: true,
        isExpressPicked: false,
        expressProfileType: '自提',
        expressProfile: '请选择自提点',
        isPriceInvalid: false,
        newPrice: '',
        detailImg: '',
        detailBtnStat: 'hover',
        recommendBtnStat: ''
    },
    onLoad: function (query) {
        var productId = commonFunc.getParam(query, 'product_id', '')
        var currentGoods = businessStorage.get('current_pick_goods')
        var distributionId = ''
        var expressId = ''
        console.log(currentGoods)
        for (var k in currentGoods.distribution_list) {
            if (currentGoods.distribution_list[k]['weapp_distribution'] == '自提') {
                distributionId = currentGoods.distribution_list[k]['id']
            } else if (currentGoods.distribution_list[k]['weapp_distribution'] == '快递') {
                expressId = currentGoods.distribution_list[k]['id']
            }
        }
        for (var k in currentGoods.logistics_location) {
            currentGoods.logistics_location[k]['pick'] = false
            currentGoods.logistics_location[k]['express_type'] = distributionId 
            currentGoods.logistics_location[k]['express_type_flag'] = '自提' 
        }
        if (expressId) {
            currentGoods.logistics_location.unshift({
                'id': '',
                'name': '',
                'weapp_user_id': '',
                'address': '物流',
                'service_charges': '',
                'express_type': expressId,
                'express_type_flag': '物流',
                'send_date': '',
                'pick': false
            })
        }
        currentGoods['goods_sum'] = 1
        currentGoods['goods_express_profile_type'] = '自提'
        currentGoods['goods_express_profile'] = '请选择自提点'
        currentGoods['description'] = currentGoods['description'].replace(/\n/, '')
        this.setData({
            detailImg: currentGoods.photo_detail1,
            goodsDetail: currentGoods
        })
    },
    onShow: function () {
        this.flashBtn();
    },
    onHide: function () {
        if (this.data.couponBtnFlashStatus) {
            //clearInterval(this.data.couponBtnFlashStatus)
        }
    },
    flashBtn: function (query) {
        if (!this.data.couponBtnFlashStatus) {
            this.data.couponBtnFlashStatus = setInterval(this.flashBtn, 100)
        } else {
            var btnColorRules = ['normal', 'green', 'yellow']
            var currentFlag = this.data.couponBtnColorFlag + 1
            if (currentFlag > 2) {
                currentFlag = 0
            }
            this.setData({
                couponBtnColorFlag: currentFlag,
                couponBtnColor: btnColorRules[currentFlag]
            })
        }
    },
    ajustGoodsSum: function (event) {
        var opeType = event.currentTarget.dataset.ope_type
        var currentGoodsSum = this.data.goodsSum
        switch (opeType) {
            case 'change':
                var newGoodsSum = event.detail.value
                if (!newGoodsSum) return false
                currentGoodsSum = newGoodsSum;
                break;
            case 'add':
                currentGoodsSum += 1
                break;
            case 'minus':
                currentGoodsSum -= 1
                currentGoodsSum = Math.max(currentGoodsSum, 1)
                if (!currentGoodsSum) {
                    this.setData({
                        goodsMinusBtnFlag: '0'
                    })
                }
                break;
        }
        var that = this
        if (currentGoodsSum > 1) {
            tools.requestWithAuth({
                url: 'product_summary',
                data: {
                    product_id: this.data.goodsDetail.id,
                    qty: currentGoodsSum
                },
                feedback: {
                    success: res => {
                        if (res.bonus_price &&
                            parseFloat(res.bonus_price) < parseFloat(that.data.goodsDetail.price)
                        ) {
                            that.setData({
                                isPriceInvalid: true,
                                newPrice: parseFloat(res.bonus_price).toFixed(2)
                            })
                        }
                    }
                }
            })
        } else {
            this.setData({
                isPriceInvalid: false
            })
        }
        that.setData({
            goodsSum: currentGoodsSum,
            'goodsDetail.goods_sum': currentGoodsSum
        })

    },
    switchExpressBox: function (event) {
        var isExpressShow = false
        if (this.data.expressBoxShow) {
            isExpressShow = false
        } else {
            isExpressShow = true
        }
        this.setData({
            expressBoxShow: isExpressShow
        })
    },
    confirmOrder: function (e) {
        var that = this
        auth.checkBind({
            success: function () {
                if (!that.data.isExpressPicked) {
                    wx.showToast({
                        title: '请选择配送方式',
                        icon: 'warning'
                    })
                    return false
                }
                var placeOrderPage = pages.placeOrder
                var currentGoods = businessStorage.save('current_pick_goods', that.data.goodsDetail)
                wx.navigateTo({
                    url: placeOrderPage
                })
            },
            fail: function () {
                wx.showToast({ title: 'fail' })
            }
        })
    },
    switchDetailBtn: function (e) {
        var switchType = e.currentTarget.dataset.type
        if (switchType == 'detail') {
            this.setData({
                detailBtnStat: 'hover',
                recommendBtnStat: '',
                detailImg: this.data.goodsDetail.photo_detail1
            })
        } else {
            this.setData({
                detailBtnStat: '',
                recommendBtnStat: 'hover',
                detailImg: this.data.goodsDetail.photo_detail2
            })
        }
    },
    pickExpress: function (e) {
        var goodsExpressType = e.currentTarget.dataset.express_type
        var goodsExpressTypeFlag = e.currentTarget.dataset.express_type_flag
        var goodsExpressId = e.currentTarget.dataset.express_id
        var goodsExpressTitle = e.currentTarget.dataset.express_title
        var goodsExpressSendDate = e.currentTarget.dataset.send_date
        var goodsExpressCharge = e.currentTarget.dataset.express_charge
        var goodsExpressOwnerId = e.currentTarget.dataset.express_owner_id
        var logisticsLocation = this.data.goodsDetail.logistics_location
        var goodsExpressProfile = ''
        var goodsExpressProfileType = '自提'
        for (var k in logisticsLocation) {
            if (logisticsLocation[k]['id'] == goodsExpressId) {
                logisticsLocation[k]['pick'] = true
            } else {
                logisticsLocation[k]['pick'] = false
            }
        }
        if (goodsExpressTypeFlag == '物流') {
            goodsExpressProfileType = '物流'
            goodsExpressProfile = '物流'
        } else {
            goodsExpressProfile = '23:00前下单，预计' + goodsExpressSendDate + '前送达'
        }
        this.setData({
            expressTitle: goodsExpressTitle,
            isExpressPicked: true,
            'goodsDetail.logistics_location': logisticsLocation,
            'goodsDetail.goods_express_charge': goodsExpressCharge,
            'goodsDetail.goods_express_type': goodsExpressType,
            'goodsDetail.goods_express_id': goodsExpressId,
            'goodsDetail.goods_express_owner_id': goodsExpressOwnerId,
            'goodsDetail.goods_express_title': goodsExpressTitle,
            'goodsDetail.goods_express_profile': goodsExpressProfile,
            'goodsDetail.goods_express_profile_type': goodsExpressProfileType,
        })
    }
})